package com.fishery.base.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.base.entity.BaseSupply;
import com.fishery.base.entity.vo.BaseSupplyAmountVo;
import com.fishery.base.entity.vo.BaseSupplyNameVo;
import com.fishery.base.mapper.BaseSupplyMapper;
import com.fishery.base.service.BaseSupplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Ghost
 * @since 2021-01-21
 */
@Service
public class BaseSupplyServiceImpl extends ServiceImpl<BaseSupplyMapper, BaseSupply> implements BaseSupplyService {

	@Autowired
	private BaseSupplyMapper baseSupplyMapper;

	@Autowired
	private ExecutorService threadPool;

	@Override
	public List<Integer> getAllType(String baseId) {
		return baseSupplyMapper.findAllSupplyType(baseId);
	}

	@Override
	public Page<BaseSupplyNameVo> pageAllSupplyByTypeAndBaseId(String baseId, int type, int size, int current) {
		Page<BaseSupplyNameVo> page = new Page<>(current, size);
		Future<List<BaseSupplyNameVo>> future = threadPool.submit(() ->
				baseSupplyMapper.findAllSupplyName(baseId, type, size, (current - 1) * size));
		int total = baseSupplyMapper.findCount(baseId, type);
		page.setTotal(total);
		page.setPages(total / size + (total % size == 0 ? 0 : 1));
		try {
			page.setRecords(future.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return page;
	}

	@Override
	public void addBaseSupplyWeight(String baseId, String supplyId, BigDecimal delt) {
		baseSupplyMapper.increaseBaseSupplyWeight(baseId, supplyId, delt);
	}

	@Override
	public void decreaseBaseSupply(String baseId, String supplyId, BigDecimal weight) {
		baseSupplyMapper.decreaseBaseSupply(baseId, supplyId, weight);
	}

	@Override
	public Page<BaseSupply> pageBaseSuppy(String baseId, int size, int current) {
		Page<BaseSupply> page = new Page<>(current, size);
		Future<List<BaseSupply>> future = threadPool.submit(() ->
				baseSupplyMapper.findBaseSupplyWithPage(baseId, (current - 1) * size, size));
		int total = baseSupplyMapper.selectTotal(baseId);
		page.setPages(total / size + (total % size == 0 ? 0 : 1));
		page.setTotal(total);
		try {
			page.setRecords(future.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return page;
	}

	@Override
	public List<BaseSupplyAmountVo> getAmountInfoOfBaseSupply(String baseId) {
		return baseSupplyMapper.findAmountInfoOfBaseSupply(baseId);
	}

	@Override
	public List<BaseSupply> listBaseSuppies(String baseId) {
		return baseSupplyMapper.findAllBaseSupplies(baseId);
	}


}
